package com.ophiux.websocket.controller;

import com.ophiux.websocket.model.InMessage;
import com.ophiux.websocket.model.OutMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * @desc: 通过 SimpMessagingTemplate.convertAndSend()
 * @author: hhl
 * @date：`2023/6/20` 11:37
 */
@Controller
@Slf4j
public class GameInfoV2Controller {
    @Autowired
    private SimpMessagingTemplate template;

    //消息路由，别人发送的消息会到这里来
    @MessageMapping("/v2/chat")
    public void gameInfo(InMessage message) {
        log.info("/v2/chat接收信息：{}", message);

        OutMessage outMessage = new OutMessage();
        BeanUtils.copyProperties(message, outMessage);
        outMessage.setTime(LocalDateTime.now());
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
        }
        //发送消息
        template.convertAndSend("/topic/game_rank", outMessage);
    }
}